package common

import (
	"context"
	"mini-app/internal/app/grpc/client"
	"mini-app/internal/app/grpc/protoc/sale_adminc"

	"192.168.1.75/go-pkg/logx"
)

type GRPC struct{}

// GoodsDetail 商品详情
func (GRPC) GoodsDetail(ctx context.Context, goodsID string) *sale_adminc.GoodsDetailRsp {
	cli, err := client.GetSaleAdminClient()
	if cli != nil && err == nil {
		rsp, err := cli.GoodsDetail(ctx, &sale_adminc.GoodsDetailReq{
			GoodsId: goodsID,
		})
		if rsp != nil && err == nil {
			return rsp
		}
	}
	return nil
}

// GetGoodsList 商品列表
func (GRPC) GetGoodsList(
	ctx context.Context,
	robotID string,
	goodsType int,
	goodsClass int,
	goodsID string,
	goodsName string,
	containerUseStatus int,
	inventoryStatus int,
) (list []*sale_adminc.RobotGoodsListRsp_RobotGoodsList, err error) {
	cli, err := client.GetSaleAdminClient()
	if cli != nil && err == nil {
		rsp, err := cli.RobotGoodsList(ctx, &sale_adminc.RobotGoodsListReq{
			RobotId:            robotID,
			GoodsType:          int32(goodsType),
			GoodsId:            goodsID,
			GoodsName:          goodsName,
			ContainerUseStatus: int32(containerUseStatus),
			InventoryStatus:    int32(inventoryStatus),
		})
		return rsp.List, err
	} else {
		logx.Error(context.Background(), "GetSaleAdminClient err", logx.Err(err))
	}
	return []*sale_adminc.RobotGoodsListRsp_RobotGoodsList{}, nil
}

type TagListT struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

// GetTagList 标签列表
func (GRPC) GetTagList(
	ctx context.Context,
	name string,
) (
	[]*TagListT,
	error,
) {
	list := []*TagListT{}
	cli, err := client.GetSaleAdminClient()
	if cli != nil && err == nil {
		rsp, err := cli.GoodsTagList(ctx, &sale_adminc.GoodsTagListReq{
			Name: name,
		})
		if rsp != nil && err == nil {
			for _, record := range rsp.List {
				list = append(list, &TagListT{
					ID:   record.Id,
					Name: record.Name,
				})
			}
		}
		return list, nil
	}
	return nil, nil
}

type CornerListT struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Position int    `json:"position"`
	Image    string `json:"image"`
}

// GetCornerLabelList 脚标
func (GRPC) GetCornerLabelList(
	ctx context.Context,
	name string,
) (
	[]*CornerListT,
	error,
) {
	list := []*CornerListT{}
	cli, err := client.GetSaleAdminClient()
	if cli != nil && err == nil {
		rsp, err := cli.GoodsCornerLableList(ctx, &sale_adminc.GoodsCornerLabelListReq{
			Name: name,
		})
		if rsp != nil && err == nil {
			for _, record := range rsp.List {
				list = append(list, &CornerListT{
					ID:       record.Id,
					Name:     record.Name,
					Position: int(record.Position),
					Image:    record.Image,
				})
			}
			return list, nil
		}
	}
	return nil, nil
}
